package com.itqianfeng.smartmeeting.config;

import com.alibaba.fastjson2.JSON;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        //向前端返回JSOn数据
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","登录失败");

        String s = JSON.toJSONString(map);
        httpServletResponse.setContentType("application/json;charset=utf-8");
        //后端向前端发送数据
        httpServletResponse.getWriter().write(s);
    }
}
